package com.example.emos.api.service.impl;

import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.db.dao.TbDeptDao;
import com.example.emos.api.db.pojo.TbDept;
import com.example.emos.api.exception.EmosException;
import com.example.emos.api.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private TbDeptDao deptDao;


    @Override
    public int insert(TbDept tbDept) {
        int isInsert = this.deptDao.insert(tbDept);
        if(isInsert<=0){
            throw new EmosException("添加失败");
        }
        return isInsert;
    }

    @Override
    public int update(TbDept tbDept) {
        int isUpdate = this.deptDao.update(tbDept);
        if(isUpdate<=0){
            throw new EmosException("修改失败");
        }
        return isUpdate;

    }

    @Override
    public int delete(Integer[] ids) {
        //TODO 如果部门人数大于0 等于1，不能删除部门，得先删除员工
        int isDelete = this.deptDao.delete(ids);
        if(isDelete<=0){
            throw new EmosException("删除失败");
        }
        return isDelete;
    }

    @Override
    public PageUtils selectDeptPage(HashMap param) {
        ArrayList<HashMap> list = this.deptDao.selectDeptPage(param);
        long count = this.deptDao.selectDeptPageCount(param);
        int start =(Integer) param.get("start");
        int length =(Integer) param.get("length");
        PageUtils pageUtils = new PageUtils(list, count, start, length);
        return pageUtils;
    }



    @Override
    public ArrayList<HashMap> searchAllDept() {
        ArrayList<HashMap> list = deptDao.searchAllDept();
        return list;
    }

    @Override
    public HashMap searchById(int id) {
        HashMap map = deptDao.searchById(id);
        return map;
    }
}
